package com.angis.fx.activity.service;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URI;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;

import com.angis.fx.activity.Enforcement;
import com.angis.fx.data.SearchCondition;
import com.angis.fx.util.DataParseUtil;

public class FieldSearchService extends Service {
	
	private int mTotalCount;

	@Override
	public void onCreate() {
		super.onCreate();
	}

	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	@Override
	public void onStart(Intent intent, int startId) {
		super.onStart(intent, startId);
//		Toast.makeText(this, "场所查询已运行!", Toast.LENGTH_LONG).show();
		try{
			handleSearchRequest((SearchCondition)intent.getExtras().get("searchcondition"));
		}catch (Exception e){
			Toast.makeText(this, "场所查询报错!", Toast.LENGTH_LONG).show();
		}finally{
			this.stopSelf();
		}
	}
	
	private void handleSearchRequest(SearchCondition pCondition) {
		HttpClient lClient = new DefaultHttpClient();
		StringBuilder lBuilder;
		HttpGet lGet = new HttpGet();
		HttpResponse lResponse;
		BufferedReader reader;
		try {
			if (mTotalCount == 0) {
				lGet.setURI(new URI(retrieveURL(pCondition).append("&").append(
						"isTotal=").append("true").toString()));
				lResponse = lClient.execute(lGet);
				lBuilder = new StringBuilder();
				reader = new BufferedReader(new InputStreamReader(lResponse
						.getEntity().getContent()));
				for (String s = reader.readLine(); s != null; s = reader.readLine()) {
					lBuilder.append(s);
				}
				getTotalCount(lBuilder.toString());
				if (mTotalCount == 0){
					Intent intent = new Intent("sendsearchresult");
					intent.putExtra("results", "");
					this.sendBroadcast(intent);
					return;
				}
			}			

			lGet.setURI(new URI(retrieveURL(pCondition).append("&").append(
					"isTotal=").append("false").toString()));
			lResponse = lClient.execute(lGet);
			lBuilder = new StringBuilder();
			reader = new BufferedReader(new InputStreamReader(lResponse
					.getEntity().getContent()));
			for (String s = reader.readLine(); s != null; s = reader.readLine()) {
				lBuilder.append(s);
			}
			lBuilder.append("::").append(mTotalCount);
			Intent intent = new Intent("sendsearchresult");
			intent.putExtra("results", lBuilder.toString());
			this.sendBroadcast(intent);
		} catch (Exception e) {
			Toast.makeText(this, "获取信息出错!", Toast.LENGTH_LONG).show();
			e.printStackTrace();
		}
	}

	private StringBuilder retrieveURL(SearchCondition pCondition) {
		StringBuilder lBuilder = new StringBuilder();
		lBuilder.append("http://").append(Enforcement.HOST).append("/ZJWHServiceTest/GIS_WHCS.asmx/GetWHCSInfo_YW").append("?");
		lBuilder.append("CON_Title=").append(pCondition.getConTitle());
		lBuilder.append("&").append("Address=").append(pCondition.getAddress());
		lBuilder.append("&").append("AreaType=").append(pCondition.getCsType());
		lBuilder.append("&").append("Area=").append(pCondition.getAreaType());
		lBuilder.append("&").append("PermitCode=").append(
				pCondition.getPermitCode());
		lBuilder.append("&").append("PermitWord=").append(
				pCondition.getPermitWord());
		lBuilder.append("&").append("pageindex=").append(pCondition.getPageIndex());
		lBuilder.append("&").append("pagesize=").append("10");
		return lBuilder;

	}


	private void getTotalCount(String response) {
		mTotalCount = Integer.parseInt(DataParseUtil.handleResponse(response));
	}
	
	

}
